package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxFile;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcInventoryService;
import com.vtuzx.web.bean.VtuzxDownloadBean;
import com.vtuzx.web.constant.ApiType;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.util.resources.is.CalendarData_is;

import java.util.List;
import java.util.Map;

/**
 * 库存
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/inventory")
@RestController
public class XqcInventoryController {

    /** 库存服务 */
    @Autowired
    private IXqcInventoryService inventoryService;

    /**
     * 根据箱号从仓库中获取物料
     */
    @PostMapping("/get_material_by_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true)
    } )
    public Object getMaterialByBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getObject(param, "locationNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        return inventoryService.getMaterialBox(stockNo, locationNo, boxNo);
    }

    /**
     * 获取即时库存列表
     */
    @PostMapping("/get_inventory")
    @VtuzxApi
    public Object getInventory(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        return inventoryService.getInventory(token, param);
    }

    /**
     * 获取即时库存列表
     */
    @PostMapping("/download_inventory")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadInventory(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return inventoryService.downloadInventory(token, param);
    }

    /**
     * 获取即时库存列表
     */
    @PostMapping("/download_inventory_tray")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadInventoryTray(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return inventoryService.downloadInventoryTray(token, param);
    }

    /**
     * 获取即时库存列表
     */
    @PostMapping("/download_inventory_total")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadInventoryTotal(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return inventoryService.downloadInventoryTotal(token, param);
    }

    /**
     * 获取即时库存列表
     */
    @PostMapping("/download_inventory_detail")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadInventoryDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return inventoryService.downloadInventoryDetail(token, param);
    }

    /**
     * 获取即时库存列表
     */
    @PostMapping("/get_inventory_detail")
    @VtuzxApi
    public Object getInventoryDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        return inventoryService.getInventoryDetail(token, param);
    }


    /**
     * 根据RFID从即时库存单中获取物料批量入库
     */
    @PostMapping("/get_material_by_rfid_in_stock")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "rfid", label = "RFID", notNull = true),
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true)
    } )
    public Object getMaterialByRfidInStock(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        List<String> rfidList = VtuzxUtil.getObject(param, "rfid");
        String stockNo = VtuzxUtil.getObject(param, "stockNo");
        String locationNo = VtuzxUtil.getObject(param, "locationNo");
        return inventoryService.getMaterialByRfidInStock(rfidList, stockNo, locationNo);
    }

    /**
     * 条件搜索批次和保质期调整单的物料列表
     */
    @PostMapping("/get_batch_and_exp_material_list")
    @VtuzxApi
    public Object getBatchAndExpMaterialList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        String batchNo = VtuzxUtil.getString(param, "batchNo");
        String materialNo = VtuzxUtil.getString(param, "materialNo");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        int exp = VtuzxUtil.getInt(param, "exp");
        return inventoryService.getBatchAndExpMaterialList(batchNo, materialNo, stockNo, exp);
    }

    /**
     * 保存批次和保质期调整单箱子
     */
    @PostMapping("/save_batch_and_exp_bill")
    @VtuzxApi
    public Object saveBatchAndExpBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        List<Map<String, Object>> boxList = VtuzxUtil.getObject(param, "boxList");
//        String updateBatchNo = VtuzxUtil.getString(param, "updateBatchNo");
//        int updateExp = VtuzxUtil.getInt(param, "updateExp");
        inventoryService.saveBatchAndExpBill(_userId, name, boxList);
        return null;
    }

    /**
     * 创建订单
     */
    @PostMapping("/create_batch_and_exp_bill")
    @VtuzxApi
    public VtuzxMap createBatchAndExpBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String updateBatchNo = VtuzxUtil.getString(param, "updateBatchNo");
        int updateExp = VtuzxUtil.getInt(param, "updateExp");
        List<Map<String, Object>> selectMaterialList = VtuzxUtil.getObject(param, "selectMaterialList");
        return inventoryService.createBatchAndExpBill(_userId, name, updateBatchNo, updateExp, selectMaterialList);
    }



    /**
     * 审核批次和保质期调整单
     */
    @PostMapping("/audit_batch_and_exp_bill")
    @VtuzxApi
    public Object auditBatchAndExpBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        return inventoryService.auditBatchAndExpBill(_userId, name, billNo);
    }


    /**
     * 反审批次和保质期调整单
     */
    @PostMapping("/unaudit_batch_and_exp_bill")
    @VtuzxApi
    public Object unauditBatchAndExpBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        return inventoryService.unauditBatchAndExpBill(_userId, name, billNo);
    }


    /**
     * 查询临时物料列表
     */
    @PostMapping("/get_batch_and_exp_material_temp_list")
    @VtuzxApi
    public Object getBatchAndExpMaterialTempList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return inventoryService.getBatchAndExpMaterialTempList(_userId, name);
    }

    /**
     * 查询临时物料列表
     */
    @PostMapping("/get_batch_and_exp_material_temp_box_list")
    @VtuzxApi
    public Object getBatchAndExpMaterialTempBoxList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String id = VtuzxUtil.getString(param, "id");
        return inventoryService.getBatchAndExpMaterialTempBoxList(_userId, name, id);
    }


    /**
     * 删除订单
     */
    @PostMapping("/delete_bill_batch_and_exp")
    @VtuzxApi
    public Object deleteBillBatchAndExp(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        inventoryService.deleteBillBatchAndExp(_userId, name, param);
        return null;
    }


    /**
     * 删除物料
     */
    @PostMapping("/delete_material_batch_and_exp")
    @VtuzxApi
    public Object deleteMaterialBatchAndExp(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        inventoryService.deleteMaterialBatchAndExp(_userId, name, param);
        return null;
    }


    /**
     * 删除箱子
     */
    @PostMapping("/delete_box_batch_and_exp")
    @VtuzxApi
    public Object deleteBoxBatchAndExp(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        inventoryService.deleteBoxBatchAndExp(_userId, name, param);
        return null;
    }


    /**
     * 获取批次和保质期调整订单列表
     */
    @PostMapping("/get_batch_and_exp_list")
    @VtuzxApi
    public Object getBatchAndExpList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return inventoryService.getBatchAndExpList(_userId, name, param);

    }

    /**
     * 获取单据头和物料
     */
    @PostMapping("/get_batch_and_exp_data")
    @VtuzxApi
    public Object getBatchAndExpData(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        return inventoryService.getBatchAndExpData(_userId, name, billNo);
    }

    /**
     * 获取即时库存批次
     */
    @PostMapping("/get_inventory_batch")
    @VtuzxApi
    public Object getInventoryBatch(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String key = VtuzxUtil.getString(param, "key");
        return inventoryService.getInventoryBatch(_userId, name, key);
    }

    /**
     * 包材出库查询
     */
    @PostMapping("/get_inventory_baocai")
    @VtuzxApi
    public Object getInventoryBaocai(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String key = VtuzxUtil.getString(param, "key");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        return inventoryService.getInventoryBaocai(_userId, name, key, stockNo);

    }

    /**
     * 获取物料台账报表列表
     * @param token
     * @param param
     * @return
     */
    @PostMapping("/get_material_summary_list")
    @VtuzxApi
    public Object getMaterialSummaryList(@VtuzxControlParam VtuzxToken token,@RequestBody Map<String,Object> param){
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return inventoryService.getMaterialSummaryList(_userId, name, param);
    }

    /**
     * 导出物料台账报表到excle
     */
    @PostMapping("/download_material_summary")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadMaterialSummary(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return inventoryService.downloadMaterialSummary(token, param);
    }

    /**
     * 物料收发明细报表
     * @param token
     * @param param
     * @return
     */
    @PostMapping("/get_material_summary_detail_list")
    @VtuzxApi
    public Object getMaterialSummaryDetailList(@VtuzxControlParam VtuzxToken token,@RequestBody Map<String,Object> param){
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return inventoryService.getMaterialSummaryDetailList(_userId, name, param);
    }

    /**
     * 导出物料收发明细报表到excle
     */
    @PostMapping("/download_material_summary_detail")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadMaterialSummaryDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return inventoryService.downloadMaterialSummaryDetail(token, param);
    }


    /**
     * 查询全部出库单
     */
    @PostMapping("/get_all_out_stock_bill")
    @VtuzxApi
    public Object getAllOutStockBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String materialNo = VtuzxUtil.getString(param, "materialNo");
        String billType = VtuzxUtil.getString(param, "billType");
        List<String> createTime = VtuzxUtil.getObject(param, "createTime");
        int pageIndex = VtuzxUtil.getInt(param, "pageIndex");
        int pageSize = VtuzxUtil.getInt(param, "pageSize");
        return inventoryService.getAllOutStockBill(_userId, name, billNo, materialNo, billType, createTime, pageIndex, pageSize);
    }


    /**
     * 手动出库
     */
    @PostMapping("/hand_out_stock")
    @VtuzxApi
    public Object handOutStock(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        inventoryService.handOutStock(_userId, name, param);
        return null;
    }

    /**
     * 传入箱号和唯一码
     */
    @PostMapping("/sync_inventory_serial")
    @VtuzxApi
    public Object syncInventorySerial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        String name = VtuzxUtil.getString(token,"name");
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        inventoryService.syncInventory(name,_userId);
        return null;
    }

    /**
     * 获取唯一码和箱码excle
     */
    @PostMapping("/download_inventory_serial")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadInventorySerial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return inventoryService.downloadInventorySerial(token, param);
    }

    /**
     * 唯一码和箱号销售出
     */
//    @PostMapping("/serial_sale_out")
//    @VtuzxApi
//    public Object serialSaleOut(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
//        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
//        String name = VtuzxUtil.getString(token, "name");
//        inventoryService.serialSaleOut(_userId,name);
//        return null;
//    }

    /**
     * 修改追溯码
     */
    @PostMapping("/change_traceCode")
    @VtuzxApi
    public VtuzxMap changeTraceCode(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return inventoryService.changeTraceCode(_userId,name,param);

    }
    /**
     * 根据唯一码获取信息
     */
    @PostMapping("/get_detail_by_trace")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "traceBackNo", label = "唯一码", notNull = true)
    } )
    public Object getDetailByTrace(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        String traceBackNo= VtuzxUtil.getString(param, "traceBackNo");

        return inventoryService.getDetailByTrace(traceBackNo);
    }

    /**
     * 根据唯一码和箱码出库
     */
    @PostMapping("/upload_sale_out")
    @VtuzxApi
    public Object uploadSaleOut(@VtuzxControlParam VtuzxToken token, @RequestParam Map<String, Object> param, @RequestParam("file") MultipartFile[] files) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        List<VtuzxFile> vFileList = VtuzxUtil.getObject(param, "files");
        inventoryService.uploadSaleOut(_userId, name, vFileList.get(0).file());
        return null;
    }


    /**
     * 根据唯一码获取信息
     */
    @PostMapping("/get_serial_up")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "traceBackNo", label = "唯一码", notNull = true)
    } )
    public Object getSerialUp(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        String traceBackNo= VtuzxUtil.getString(param, "traceBackNo");
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);

        return inventoryService.getSerialUp(_userId,traceBackNo);
    }

    /**
     * 根据批次号上查
     */
    @PostMapping("/get_batchNo_up")
    @VtuzxApi()
    public Object getBatchNoUp(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        String batchNo= VtuzxUtil.getString(param, "batchNo");
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);

        return inventoryService.getBatchNoUp(_userId,batchNo);
    }


    /**
     * 获取唯一码列表(根据物料编码和批次查询吉客云单号)
     */
    @PostMapping("/get_traceability_code_list")
    @VtuzxApi
    public Object getTraceabilityCodeList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        return inventoryService.getTraceabilityCodeList(token, param);
    }

    /**
     * 获取损耗率的报表(成品)
     * @param token
     * @param param
     * @return
     */
    @PostMapping("/get_loss_rote_list")
    @VtuzxApi
    public Object getLossRoteList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param){
        return inventoryService.getLossRoteList(token,param);
    }

    /**
     * 下载损耗率报表
     */
    @PostMapping("/download_loss_rote_list")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadLossRoteList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return inventoryService.downloadLossRoteList(token, param);
    }

    /**
     * 获取定时同步的库存
     */
    @PostMapping("/get_regular")
    @VtuzxApi
    public Object getRegular(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        return inventoryService.getRegular(token, param);
    }

    /**
     * 导出定时库存列表
     */
    @PostMapping("/download_regular")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadRegular(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return inventoryService.downloadRegular(token, param);
    }


    /**
     * 根据唯一码或者批次号或者生产任务查询吉客云销售单号
     */
    @PostMapping("/get_serial_trade")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "traceBackNo", label = "唯一码", notNull = true)
    } )
    public Object getSerialTrade(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        String traceBackNo= VtuzxUtil.getString(param, "traceBackNo");
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);

        return inventoryService.getSerialTrade(_userId,traceBackNo);
    }


    /**
     * 下载追溯上查信息
     */
    @PostMapping("/download_serial_trade")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadSerialTrade(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        String traceBackNo= VtuzxUtil.getString(param, "traceBackNo");
        return inventoryService.downloadSerialTrade(token, traceBackNo);
    }
}
